package com.zhiyou100.action;

import com.alibaba.fastjson.JSONObject;
import com.zhiyou100.entity.Exam;
import com.zhiyou100.service.ExamService;
import com.zhiyou100.util.ExamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

/**
 * @author yang
 * @className ExamAction
 * @packageName com.zhiyou100.action
 * @Description TODO exam action
 * @date 2020-08-26 14:52
 */
@Controller
@RequestMapping("/exam")
public class ExamAction {
    private ExamService examService;

    @Autowired
    public void setExamService(ExamService examService) {
        this.examService = examService;
    }

    @RequestMapping("/getAll.action")
    @ResponseBody
    public List<Exam> getExamServices(Model model) {
        final List<Exam> exams = examService.listExams();
        System.out.println(exams);
        return exams;
    }

    @RequestMapping("/getOneByEid.action")
    @ResponseBody
    public Exam getOneByEid(Model model, Integer eid) {
        return examService.getOneByEid(eid);
    }


    @RequestMapping("/getTextByEid.action")
    @ResponseBody
    public String getTestByEidMethod(Integer eid, HttpServletRequest request, HttpServletResponse response) throws Exception {
        final Exam oneByEid = examService.getOneByEid(eid);
        final String path = request.getServletContext().getInitParameter("path");

        ExamUtil.creatText(oneByEid, path);
        final JSONObject jsonObject = new JSONObject();
        String message = "添加成功！，位置" + path;
        jsonObject.put("message", message);
        return jsonObject.toJSONString();

    }
}
